package com.shiminghui.project;

import java.util.ArrayList;
import java.util.Scanner;


public class view {



    public static void main(String[] args) {


        ArrayList<Student> students = new ArrayList<>();
        manage(students);
    }

    public static void show(){
        System.out.println("--------学生管理系统---------");
        System.out.println("1:添加学生");
        System.out.println("2:删除学生");
        System.out.println("3:修改学生");
        System.out.println("4:查询学生");
        System.out.println("5:退出");
        System.out.println("输入您的选择:");
    }

    public static boolean add(ArrayList<Student> list){
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        System.out.println("请输入学生id:");
        student.setId(scanner.next());
        while (contains(list,student.getId())){
            System.out.println("已经存在此id,请重新输入id");
            student.setId(scanner.next());
        }
        System.out.println("请输入学生姓名:");
        student.setName(scanner.next());
        System.out.println("请输入学生年龄:");
        student.setAge(scanner.nextInt());
        System.out.println("请输入学生家庭地址:");
        student.setAddress(scanner.next());
        list.add(student);
        System.out.println("添加成功");
        return false;
    }

    public static boolean delete(ArrayList<Student> list){
        if (!isempty(list))
            return false;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要删除学生的id:");
        String id = scanner.next();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)){
                list.remove(list.get(i));
                System.out.println("删除成功");
                return true;
            }
        }
        System.out.println("没有该学生,删除失败.");
        return false;
    }

    public static boolean update(ArrayList<Student> list){
        if (!isempty(list))
            return false;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要修改学生的id:");
        String id = scanner.next();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)){
                System.out.println("请输入学生id:");
                list.get(i).setId(scanner.next());
                while (contains(list,list.get(i).getId())){
                    System.out.println("已经存在此id,请重新输入id");
                    list.get(i).setId(scanner.next());
                }
                System.out.println("请输入学生姓名:");
                list.get(i).setName(scanner.next());
                System.out.println("请输入学生年龄:");
                list.get(i).setAge(scanner.nextInt());
                System.out.println("请输入学生家庭地址:");
                list.get(i).setAddress(scanner.next());
                System.out.println("修改成功");
                return true;
            }
        }
        System.out.println("没有该学生,修改失败.");
        return false;
    }

    public static boolean query(ArrayList<Student> list){
        if (!isempty(list))
            return false;
        System.out.println("id\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.print(student.getId()+"\t");
            System.out.print(student.getName()+"\t");
            System.out.print(student.getAge()+"\t");
            System.out.println(student.getAddress());
        }
        return true;
    }

    public static boolean isempty(ArrayList<Student> list){
        if (list.isEmpty()){
            System.out.println("没有数据");
            return false;
        }
        return true;
    }

    public static boolean contains(ArrayList<Student> list,String id){
        if (list.isEmpty()){
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)){
                return true;
            }
        }
        return false;
    }

    public static void manage(ArrayList<Student> students){
        Scanner scanner = new Scanner(System.in);

        loop: while (true){
            show();
            String a = scanner.next();
            switch (a){
                case "1":
                    add(students);
                    break ;
                case "2":
                    delete(students);
                    break ;
                case "3":
                    update(students);
                    break ;
                case "4":
                    query(students);
                    break ;
                case "5":
                    break loop;
                //System.exit(0);//停止虚拟机运行.
                default:
                    System.out.println("没有此选项");
            }
        }
    }



}


